package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daxidi.cn.entity.Notice;
import com.daxidi.cn.entity.Shop;
import com.daxidi.cn.mapper.NoticeMapper;
import com.daxidi.cn.mapper.ShopMapper;
import com.daxidi.cn.service.INoticeService;
import com.daxidi.cn.vo.NoticeVo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 工会小程序公告信息表 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-12-04
 */
@Service
@Log4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private ShopMapper shopMapper;

    //  发布
    @Override
    public void createNotice(Notice notice) {
        notice.setCreatedAt(new Date());
        notice.setUpdatedAt(null);
        notice.setState(1);
        if (notice.getTop() == 1) {
            QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
            noticeQueryWrapper.eq("top", 1);
            List<Notice> notices = noticeMapper.selectList(noticeQueryWrapper);
            for (Notice n : notices) {
                n.setTop(2);
                noticeMapper.updateById(n);
            }
        }
        noticeMapper.insert(notice);
    }

    //  下线
    @Override
    public void updateState(Integer id) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Notice notice = noticeMapper.selectOne(wrapper);
        notice.setUpdatedAt(new Date());
        notice.setState(0);
        noticeMapper.updateById(notice);
    }

    // 上线
    @Override
    public void upState(Integer id) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Notice notice = noticeMapper.selectOne(wrapper);
        notice.setUpdatedAt(new Date());
        notice.setState(1);
        noticeMapper.updateById(notice);
    }

    //  编辑
    @Override
    public void updateNotice(Notice notice) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", notice.getId());
        notice.setUpdatedAt(new Date());
        if (notice.getTop() == 1) {
            QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
            noticeQueryWrapper.eq("top", 1);
            List<Notice> notices = noticeMapper.selectList(noticeQueryWrapper);
            for (Notice n : notices) {
                n.setTop(2);
                noticeMapper.updateById(n);
            }
        }
        noticeMapper.updateById(notice);
    }

    //  查询上线
    @Override
    public Map selectAllNotices(Integer pageNum, Integer pageSize) {
        Integer page = (pageNum - 1) * pageSize;
        Integer limit = pageNum * pageSize;
        List<Notice> notices = noticeMapper.showAllNotices(page, limit);
        Map map = new HashMap();
        map.put("count", notices.size());
        map.put("data", notices);
        return map;
    }

    //  查询上线
    @Override
    public Map selectAllNotices2(Integer pageNum, Integer pageSize) {
        Integer pageNo = (pageNum - 1) * pageSize;
        Page<Notice> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("state", 1);
        noticeQueryWrapper.orderByAsc("top");
        noticeQueryWrapper.orderByDesc("created_at");
        Page<Notice> noticePage = noticeMapper.selectPage(page, noticeQueryWrapper);
        List<Notice> noticeList = noticePage.getRecords();
        List<NoticeVo> noticeVoList = new ArrayList<>();
        if (noticeList.size() > 0) {
            for (Notice notice : noticeList) {
                NoticeVo noticeVo = new NoticeVo();
                BeanUtils.copyProperties(notice, noticeVo);
                Shop shop = shopMapper.selectById(notice.getType());
                noticeVo.setShopName(shop.getName());
                noticeVoList.add(noticeVo);
            }
        }
        Map map = new HashMap();
        map.put("count", noticePage.getTotal());
        map.put("data", noticeVoList);
        return map;
    }

    //  查询下线
    @Override
    public Map selectAll(Integer pageNum, Integer pageSize) {
        Integer pageNo = (pageNum - 1) * pageSize;
        Page<Notice> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("state", 0);
        noticeQueryWrapper.orderByAsc("top");
        noticeQueryWrapper.orderByDesc("created_at");
        Page<Notice> noticePage = noticeMapper.selectPage(page, noticeQueryWrapper);
        List<Notice> noticeList = noticePage.getRecords();
        List<NoticeVo> noticeVoList = new ArrayList<>();
        if (noticeList.size() > 0) {
            for (Notice notice : noticeList) {
                NoticeVo noticeVo = new NoticeVo();
                BeanUtils.copyProperties(notice, noticeVo);
                Shop shop = shopMapper.selectById(notice.getType());
                noticeVo.setShopName(shop.getName());
                noticeVoList.add(noticeVo);
            }
        }
        Map map = new HashMap();
        map.put("count", noticePage.getTotal());
        map.put("data", noticeVoList);
        return map;
    }

    //  查询下线
    @Override
    public Map selectAll2(Integer pageNum, Integer pageSize) {
        Integer page = (pageNum - 1) * pageSize;
        Integer limit = pageNum * pageSize;
        List<Notice> notices = noticeMapper.showAll(page, limit);
        Map map = new HashMap();
        map.put("count", notices.size());
        map.put("data", notices);
        return map;
    }

    //  置顶
    @Override
    public void setTop(Integer id) {
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("top", 1);
        List<Notice> notices = noticeMapper.selectList(noticeQueryWrapper);
        for (Notice n : notices) {
            n.setTop(2);
            noticeMapper.updateById(n);
        }
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Notice notice = noticeMapper.selectOne(wrapper);
        notice.setCreatedAt(new Date());
        notice.setTop(1);
        noticeMapper.updateById(notice);
    }

    //  取消置顶
    @Override
    public void cancelTop(Integer id) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Notice notice = noticeMapper.selectOne(wrapper);
        notice.setUpdatedAt(new Date());
        notice.setTop(2);
        noticeMapper.updateById(notice);
    }
}
